package com.ttl.stock.action;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


import com.ttl.bc.report.receive.material.ReportReceiveMaterialBO;
import com.ttl.stock.db.ITTL_MonthlyClosed;
import com.ttl.stock.db.StockBO;
import com.ttl.stock.db.StockDAO;
import com.ttl.view.ITTL_OutStock;

public class StockAction extends DispatchAction {
	
	@SuppressWarnings("unchecked")
	public ActionForward report_outStock(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
			
		String param = (String)request.getParameter("param");
		
		if(param==null) {
			
/*			String hql = "select distinct inventoryDate from ITTL_MonthlyInventory where closedStatus = 1 order by inventoryDate";
			
			List list = new StockDAO().find(hql);
			System.out.println(hql);
			Calendar ca1 = Calendar.getInstance();
			
			ArrayList yearMonthList = new ArrayList();
			for (int i = 1; i < list.size(); i++) {// ไม่เอาเดือน 6 ปี 2012 เพราะเป็นเดือนตั้งต้น begin
 				Date date = (Date)list.get(i);
 				
				ca1.set(date.getYear(), date.getMonth(),1);
				java.util.Date d = new java.util.Date(ca1.getTimeInMillis());
				
				yearMonthList.add(new YearMonthReport(""+(date.getYear()+1900)+"-"+new SimpleDateFormat("MMMM").format(d), 
						""+(date.getYear()+1900)+"-"+date.getMonth()));
				
			}
			
			request.setAttribute("yearMonthList", yearMonthList);*/						
			
			String hql = "select distinct  yearReport, monthReport " +
					" from ITTL_MonthlyInventoryReport order by yearReport, monthReport";
			
			hql = //"select distinct  yearReport, monthReport " +
					" from ITTL_MonthlyClosed " +
					" where closedStatus=1 "+
					" order by yearReport, monthReport";
			
			List list = new StockDAO().find(hql);
			System.out.println(hql);
			Calendar ca1 = Calendar.getInstance();
			
			ArrayList<YearMonthReport> yearMonthList = new ArrayList<YearMonthReport>();
			for (int i = 1; i < list.size(); i++) {// ไม่เอาเดือน 6 ปี 2012 เพราะเป็นเดือนตั้งต้น begin 				
/* 				
				Object[] tuple = (Object[])list.get(i);
				int yearReport = Integer.valueOf(tuple[0].toString());
				int monthReport = Integer.valueOf(tuple[1].toString());
				
*/
				ITTL_MonthlyClosed mc = (ITTL_MonthlyClosed)list.get(i);
				int yearReport	= mc.getYearReport();
				int monthReport	= mc.getMonthReport();
				
				
					ca1.set(yearReport, monthReport-1,1);
					java.util.Date d = new java.util.Date(ca1.getTimeInMillis());
					
					yearMonthList.add(new YearMonthReport(""+(yearReport)+"-"+new SimpleDateFormat("MMMM").format(d), 
							""+(yearReport)+"-"+monthReport));								
			}
			
			request.setAttribute("yearMonthList", yearMonthList);
			
			System.out.println("---------------------------------------");
			
			hql = //"select distinct  yearReport, monthReport " +
					" from ITTL_MonthlyClosed " +
					" where closedStatus=0 "+
					" order by yearReport, monthReport";
			
			list = new StockDAO().find(hql);
			System.out.println(hql);
			ca1 = Calendar.getInstance();
			
			ArrayList<YearMonthReport> yearMonthList2 = new ArrayList<YearMonthReport>();
			
			if(list!=null && !list.isEmpty()) {/*				
				Object[] tuple = (Object[])list.get(list.size()-1);
				int yearReport = Integer.valueOf(tuple[0].toString());
				int monthReport = Integer.valueOf(tuple[1].toString());
							*/
				ITTL_MonthlyClosed mc = (ITTL_MonthlyClosed)list.get(0);
				int yearReport	= mc.getYearReport();
				int monthReport	= mc.getMonthReport();				
								
				ca1.set(yearReport, monthReport-1,1);
				java.util.Date d = new java.util.Date(ca1.getTimeInMillis());
				
				yearMonthList2.add(new YearMonthReport(""+(yearReport)+"-"+new SimpleDateFormat("MMMM").format(d), 
						""+(yearReport)+"-"+monthReport));				
			}			
			request.setAttribute("yearMonthList2", yearMonthList2);
			
			// start
			hql = "select distinct year(docDate), month(docDate) " +
					"from ITTL_OutStock " +
					"where docDate is not null " +
					"order by year(docDate) desc, month(docDate) desc";
			List outList = new StockDAO().find(hql);
			ArrayList<YearMonthReport> yearMonthList3 = new ArrayList<YearMonthReport>();
			
			if(outList!=null && !outList.isEmpty()) {
				for (int i = 0; i < outList.size(); i++) { 		
					Object[] tuple = (Object[])outList.get(i);
					int yearReport = Integer.valueOf(tuple[0].toString());
					int monthReport = Integer.valueOf(tuple[1].toString());
																
					ca1.set(yearReport, monthReport-1,1);
					java.util.Date d = new java.util.Date(ca1.getTimeInMillis());
					
					yearMonthList3.add(new YearMonthReport(""+(yearReport)+"-"+new SimpleDateFormat("MMMM").format(d), 
							""+(yearReport)+"-"+monthReport));									
				}

			}			
			request.setAttribute("yearMonthList3", yearMonthList3);			
			// end
					
			
			return mapping.findForward("inventoryReport");
		}		
		long startTime = System.currentTimeMillis();
		
		if( new StockBO().getMaterialOutStockReport2(form, request, response, getServlet())) {
			System.out.println("report outstock  2 generated");
		}
		
//		new PurchasingReportBO().getDyestuffsChemicalsReport(form, request, response, getServlet());			
/*		if( new StockBO().getMaterialOutStockReport(form, request, response, getServlet())==true ) {
			System.out.println("report outstock generated");
		}else {
			System.out.println("report outstock not generate");
		}*/
		long endTime = System.currentTimeMillis();
		@SuppressWarnings("unused")
		double interval = (endTime-startTime)/1000d;
		

		
		return null;					
	}
	
	@SuppressWarnings("unused")
	public ActionForward report_monthlyInventory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
			
		String param = (String)request.getParameter("param");

		if(param==null) {
			return mapping.findForward("inventoryReport");
		}	
		
		
		
 		
//		if (new StockBO().getMonthlyInventoryReport(form, request, response, getServlet())==true) {
		if (new StockBO().getMonthlyInventoryReport2(form, request, response, getServlet())==true) {			
			System.out.println("report monthly inventory generated");
		}else {
			System.out.println("report monthly inventory not generated");
		}
						
		return null;					
	}

	public ActionForward inventoryMonthlyClosed(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String hql = "select distinct  yearReport, monthReport " +
				" from ITTL_MonthlyInventoryReport order by yearReport, monthReport";
		
		List list = new StockDAO().find(hql);
		System.out.println(hql);
		Calendar ca1 = Calendar.getInstance();
		
		ArrayList<YearMonthReport> yearMonthList3 = new ArrayList<YearMonthReport>();
		
		if(list!=null && !list.isEmpty()) {
			
			Object[] tuple = (Object[])list.get(list.size()-1);
			int yearReport = Integer.valueOf(tuple[0].toString());
			int monthReport = Integer.valueOf(tuple[1].toString());
			
							
			ca1.set(yearReport, monthReport-1,1);
			java.util.Date d = new java.util.Date(ca1.getTimeInMillis());
			
			yearMonthList3.add(new YearMonthReport(""+(yearReport)+"-"+new SimpleDateFormat("MMMM").format(d), 
					""+(yearReport)+"-"+monthReport));
			
		}
		
		request.setAttribute("yearMonthList3", yearMonthList3);	
		
		return mapping.findForward("inventoryMonthlyClosed");								
	}		
	
	public ActionForward monthlyClosed(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
			

		if (new StockBO().monthlyClosed(form, request, response, getServlet())) {
			request.setAttribute("message", "success");
			return mapping.findForward("success_monthlyClosed");
		}else {
			request.setAttribute("message", "failure");
			return mapping.findForward("failure_monthlyClosed");
		}													
	}	
}
